# -*- coding: utf-8 -*-
# @Time    : 2021/4/27 19:21
# @Author  : mokez
# @Email   : mokez@qq.com
# @File    : 高阶函数作业.py
# @Software: PyCharm
#
from functools import reduce
import re


# 利用map批量检测用户名是否符合规范=> username符合规范username不符合规范·只能包含数据、字母、下划线，不以数字开头，长度在6-18之间
# filter把合法的用户名显示出来
def map_func(username):
    if re.match(r'^[a-z]|[A-Z]|_', username) and 6 <= len(username) <= 18:
        return username
    else:
        return False


print(list(filter(map_func, ['mokez', 'mokez123456', '11mokez', '_mokez12345'])))
# 匿名函数+reduce实现1*2*3*...*n
print(reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]))


# filter函数找出[1-100]能被3整除的数
def func(x):
    if x % 3 == 0:
        return x


a = []
for i in range(1, 100):
    a.append(i)
print(list(filter(func, a)))
# map函数:求list[1-10]各项的平方
print(list(map(lambda x: x ** 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))


# 将列表[1,4,5,9]转换成'1459'的字符串
def func1(x, y):
    return x * 10 + y


result = reduce(func1, [1, 4, 5, 9])
print(str(result))
print(type(str(result)))
# 有以下列表:list1=[7,-8,5,4,0，-2,-5]
# ·正数在前负数在后
# ·整数从小到大
# ·负数从大到小
list1 = [7, -8, 5, 4, 0, -2, -5]
lst_pos = []
lst_minus = []
for i in list1:
    if i >= 0:
        lst_pos.append(i)
    elif i < 0:
        lst_minus.append(i)
# result1=sorted(list1, reverse=True)
# print(result1)
print(sorted(lst_pos) + sorted(lst_minus, reverse=True))
# 这是一个字符串排序，排序规则:小写<大写<奇数<偶数
# . s = 'asdf234GDSdsf23'
# 排序:小写-大写-奇数-偶数
# ·原理∶先比较元组的第一个值，FALSE
s = 'asdf234GDSdsf23'
lowercase = []
uppercase = []
odd_num = []
even_num = []
for i in s:
    if re.search(r"[a-z]", i):
        lowercase.append(i)
    elif re.match(r"[A-Z]", i):
        uppercase.append(i)
    elif int(i) % 2 == 1:
        odd_num.append(i)
    elif int(i) % 2 == 0:
        even_num.append(i)
print("".join(sorted(lowercase) + sorted(uppercase) + sorted(odd_num) + sorted(even_num)))
